package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author faiz
 * @since 2021-03-24
 */
@Slf4j
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {
    //把service注入进来
    @Autowired
    private EduTeacherService teacherService;

    //1 查询所有讲师的数据
    // rest风格,查询:GET,修改:PUT,删除:DELETE,添加:POST
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
//        int a = 100 / 0;
        List<EduTeacher> list = teacherService.list(null);
//        if (list != null) {
//            throw new GuliException(20001, "执行了自定义异常");
//        }
        return R.ok().data("items", list);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "根据ID删除讲师(逻辑删除)")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                           @PathVariable(name = "id") String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询讲师的方法
    @ApiOperation(value = "讲师分页")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable(name = "current") long current,
                             @PathVariable(name = "limit") long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法，底层封装,把所有分页数据封装到pageTeacher对象中
        teacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal(); //总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

    //条件查询带分页
    @ApiOperation(value = "讲师条件查询分页")
    @PostMapping("/pageTeacherCondition/{current}/{limit}") //注意，使用RequestBody必须使用post
    public R pageTeacherCondition(@PathVariable(name = "current") long current,
                                  @PathVariable(name = "limit") long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        //log.info("teacherQuery:" + teacherQuery);
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            //构建条件
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //构建条件
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //构建条件
            wrapper.le("gmt_create", end);
        }
        //按照添加时间来排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询
        teacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合
        return R.ok().data("total", total).data("rows", records);

    }

    //添加讲师接口的方法
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation(value = "根据id查询教师")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable(name = "id") String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    //讲师修改功能
//    @PostMapping("/updateTeacher")
//    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
//        boolean flag = teacherService.updateById(eduTeacher);
//        if (flag) {
//            return R.ok();
//        } else {
//            return R.error();
//        }
//    }
    @ApiOperation(value = "根据id更新教师信息")
    @PutMapping("/updateTeacher")
    public R updateTeacherById(
//            @ApiParam(name = "id", value = "讲师ID", required = true)
//            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
//        teacher.setId(id);
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}

